package cn.wolfcode.wolf2w.conteoller;


import cn.wolfcode.wolf2w.annontation.RequireLogin;
import cn.wolfcode.wolf2w.annontation.UserParam;
import cn.wolfcode.wolf2w.domain.*;
import cn.wolfcode.wolf2w.query.TravelCommentQuery;
import cn.wolfcode.wolf2w.query.TravelQuery;
import cn.wolfcode.wolf2w.redis.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.util.UMEditorUploader;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@RequestMapping("/travels")
public class TravelController {

    @Autowired
    private ITravelService travelService ;
    @Autowired
    private IDestinationService destinationService ;
    @Autowired
    private IUserInfoRedisService userInfoRedisService ;
    @Autowired
    private ITravelCommentService travelCommentService;

    @GetMapping("/query")
    public Object query(TravelQuery qo){
        Page page = travelService.query(qo);
        return JsonResult.success(page);
    }

    @GetMapping("/detail")
    public JsonResult getDetail(String id){
        Travel detail = travelService.get(id);
        System.out.println(detail);
        return JsonResult.success(detail);
    }

    //上传图片
    @RequestMapping("/contentImage")
    @ResponseBody
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception{
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize()
                +", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";
        result = result.replaceAll( "\\\\", "\\\\" );
        if(callback == null ){
            return result ;
        }else{
            return "<script>"+ callback +"(" + result + ")</script>";
        }
    }


    @RequireLogin
    @PostMapping("/saveOrUpdate")
    public Object saveOrUpdate(Travel param,HttpServletRequest request){
        String token = request.getHeader("token");
        UserInfo user = userInfoRedisService.getUserByToken(token);
        if(user!=null){
           param.setUserId(user.getId());
        }
        String id = travelService.saveOrUpdate(param);
        return JsonResult.success(id);
    }

    @GetMapping("/getDests")
    public JsonResult getDests(){
        List<Destination> dests = destinationService.list();
        return JsonResult.success(dests);
    }

    @GetMapping("/info")
    public Object getUserInfo(@UserParam UserInfo user) {
        return JsonResult.success(user);
    }

    @GetMapping("/update")
    public Object update(UserInfo user){
        return JsonResult.success(user);
    }

    @PostMapping("/commentAdd")
    public Object commentAdd(TravelComment comment, @UserParam UserInfo userInfo){
        BeanUtils.copyProperties(userInfo,comment );
        comment.setUserId(userInfo.getId());
        travelCommentService.commentAdd(comment);
        return JsonResult.success();
    }

    @GetMapping("/comments")
    public Object getComments(TravelCommentQuery qo){
        List<TravelComment> comments = travelCommentService.getComments(qo);
        return JsonResult.success(comments);
    }
}
